<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/9
 * Time: 9:57
 */

namespace app\common\library;

use think\facade\Request;

class RequestUtil
{

    /**
     * @var object  当前调用类实例
     */
    private static $instance = null;

    private $request_data;

    /**
     * @var curl curl实例
     */
    protected $curl;

    const REQUEST_GET = 1;
    const REQUEST_POST = 2;
    const REQUEST_POST_JSON = 3;


    /**
     * 初始化curl函数
     */
    public function __construct()
    {
        $this->curl = curl_init();
        if (empty($this->request_data)) {
            $this->request_data = \request()->param();
        }
    }

    /**
     * @return object|static 得到一个调用类的实例
     */
    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new static();
        }
        return self::$instance;
    }


    public function setRequestData($key, $val)
    {
        if (is_array($key)) {
            $this->request_data = $key;
        } else {
            $this->request_data[$key] = $val;
        }
    }

    public function getRequestData($key = '')
    {
        return empty($key) ? $this->request_data : $this->request_data[$key];
    }


    /**
     * @param $key
     * @param null $value
     * 向curl函数注入请求参数
     */
    public function setOption($key, $value = null)
    {
        if ($value != null) {
            $data[$key] = $value;
        } else {
            $data = $key;
        }
        foreach ($data as $key => $value) {
            curl_setopt($this->curl, $key, $value);
        };
    }

    /*
     * 发送数据给服务商
     */
    public function sendRequest($url, $param, $type = self::REQUEST_GET, $ssl = false)
    {
        //  根据请求方式的不同组合参数
        if ($type === self::REQUEST_POST) {
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
            curl_setopt($this->curl, CURLOPT_POST, true);
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $param);
        } elseif ($type === self::REQUEST_POST_JSON) {
            curl_setopt($this->curl, CURLOPT_POST, true);
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($param));
            curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        } else {
            curl_setopt($this->curl, CURLOPT_POST, false);
            if (!empty($param)) $url = ($url . "?" . http_build_query($param));
        }
        //  是否开启SSL验证
        if ($ssl === true) {
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYSTATUS, false);
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
        }
        //  设置返回值
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        //  设置请求URL
        curl_setopt($this->curl, CURLOPT_URL, $url);
        //  执行
        $result = curl_exec($this->curl);
        if ($result === false) {
            return curl_error($this->curl);
        }
        return $result;
    }


    /**
     * @return curl
     * 得到一个curl函数实例
     */
    public function getCurl()
    {
        return $this->curl;
    }


}


?>